package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.topscore.uke.R;
import com.topscore.uke.entities.UkePerformanceData;
import com.topscore.uke.mvp.contractors.GetUkePerformanceContractor;
import com.topscore.uke.mvp.presenters.GetUkePerformancePresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.ui.loadsir_callback.ErrorCallBack;
import com.topscore.uke.ui.loadsir_callback.LoadingCallBack;

import java.text.DecimalFormat;

import butterknife.Bind;
import butterknife.ButterKnife;

public class UkePerformanceActivity extends ToolBarCompatActivity implements GetUkePerformanceContractor.View {

    @Bind(R.id.one_to_one_tv)
    TextView oneToOneTv;
    @Bind(R.id.give_money_tv)
    TextView giveMoneyTv;
    @Bind(R.id.sales_amount_tv)
    TextView salesAmountTv;
    @Bind(R.id.root_container)
    LinearLayout rootContainer;
    @Bind(R.id.one_to_one_layout)
    RelativeLayout oneToOneLayout;
    @Bind(R.id.sale_performance_layout)
    RelativeLayout salePerformanceLayout;

    private DecimalFormat amountDecimalFormat = new DecimalFormat("0.00");

    private GetUkePerformanceContractor.Presenter mPresenter;
    private LoadService mLoadService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uke_performance);
        ButterKnife.bind(this);
        setCentralTitle("我的业绩");
        mPresenter = new GetUkePerformancePresenter(this);
        initView();
    }

    private void initView() {
        mLoadService = LoadSir.getDefault().register(rootContainer, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {

            }
        });
        oneToOneLayout.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, BuyOneGetOneActivity.class);
            startActivity(intent);
        });
        salePerformanceLayout.setOnClickListener(view -> {
            Intent intent = new Intent(mContext,MySalePerformanceActivity.class);
            startActivity(intent);
        });
        initData();
    }

    private void initData() {
        mPresenter.getPerformanceData();
    }

    @Override
    public void onGetDataSuccess(UkePerformanceData data) {
        oneToOneTv.setText(String.valueOf(data.getOneToOnetimes()));
        giveMoneyTv.setText("￥" + amountDecimalFormat.format(data.getGiveMoney()));
        salesAmountTv.setText("￥" + amountDecimalFormat.format(data.getSales()));
    }

    @Override
    public void onLoading(String loadingMessage) {
        mLoadService.showCallback(LoadingCallBack.class);
    }

    @Override
    public void onLoadFinished() {
        mLoadService.showSuccess();
    }

    @Override
    public void onFail(int code, String errorMessage) {
        mLoadService.showCallback(ErrorCallBack.class);
        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
    }
}
